/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.server.action.domain;


import han.bifang.business.domain.DomainManager;
import han.bifang.business.domain.ServiceBusManager;
import han.bifang.common.entity.BusBindingWrapper;
import han.bifang.common.entity.SoaBusDetail;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.BusBinding;
import han.bifang.gwt.client.common.entity.BusInnerService;
import han.bifang.gwt.client.common.entity.HttpBinding;
import han.bifang.gwt.client.common.entity.JmsBinding;
import han.bifang.gwt.client.common.entity.JmsBindingWrapper;
import han.bifang.gwt.client.common.entity.NormalJms;
import han.bifang.gwt.client.common.entity.ServiceBus;
import han.bifang.gwt.client.common.entity.SoapBinding;
import han.bifang.gwt.server.action.BifangGWTAction;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

/**
 * 
 * @Author Kent
 */
public class GetBusDetailAction implements BifangGWTAction {

	
	@Override
	public ResponseObject execute(RequestObject req,HttpServletRequest request,HttpServletResponse response) throws BifangException {
		ResponseObject res = new ResponseObject();
		try {
			String id = (String)req.getRequest();
			SoaBusDetail d = ServiceBusManager.getServiceBusDetail(id);
			//Log logger = LogFactory.getLog(GetBusDetailAction.class);
			//logger.info(d.getServiceBus());
			ArrayList ret = new ArrayList();
			ServiceBus sc = new ServiceBus();
			BeanUtils.copyProperties(sc, d.getServiceBus());
			ret.add(sc);
			ArrayList bindings = new ArrayList();
			for(int i = 0 , j = d.getBindings().size(); i < j ; i++){
				BusBindingWrapper cbw = (BusBindingWrapper)d.getBindings().get(i);
				BusBinding cb = new BusBinding();
				BeanUtils.copyProperties(cb,cbw.getBusBinding());
				if(cbw.getBusBinding().getType().equals(EntityConstants.BINDING_HTTP)){
					HttpBinding hb = new HttpBinding();
					BeanUtils.copyProperties(hb, cbw.getHttpBinding());
					cb.setBinding(hb);
				}else if(cbw.getBusBinding().getType().equals(EntityConstants.BINDING_JMS)){
					JmsBindingWrapper w = new JmsBindingWrapper();
					JmsBinding hb = new JmsBinding();
					NormalJms jj = new NormalJms();
					BeanUtils.copyProperties(hb, cbw.getJmsBinding().getJmsBinding());
					BeanUtils.copyProperties(jj, cbw.getJmsBinding().getInfo());
					w.setInfo(jj);
					w.setJmsBinding(hb);
					cb.setBinding(w);
				}else if(cbw.getBusBinding().getType().equals(EntityConstants.BINDING_SOAP)){
					SoapBinding hb = new SoapBinding();
					BeanUtils.copyProperties(hb, cbw.getSoapBinding());
					cb.setBinding(hb);
				}else if(cbw.getBusBinding().getType().equals(EntityConstants.BINDING_WEB_SERVICES)){
					SoapBinding hb = new SoapBinding();
					BeanUtils.copyProperties(hb, cbw.getSoapBinding());
					cb.setBinding(hb);
				}
				bindings.add(cb);
				
			}
			ret.add(bindings);
			BusInnerService in = new BusInnerService();
			in.setGetResultServiceCode(d.getGetResultService().getService().getServiceCode());
			SoapBinding bb = new SoapBinding();
			BeanUtils.copyProperties(bb, d.getGetResultService().getSoapBinding());
			in.setSoapBinding(bb);
			
			JmsBindingWrapper jw = new JmsBindingWrapper();
			JmsBinding jb = new JmsBinding();
			NormalJms nj = new NormalJms();
			BeanUtils.copyProperties(jb, d.getNotifyService().getJmsBinding().getJmsBinding());
			BeanUtils.copyProperties(nj, d.getNotifyService().getJmsBinding().getInfo());
			jw.setInfo(nj);
			jw.setJmsBinding(jb);
			in.setNotifyServiceCode(d.getNotifyService().getService().getServiceCode());
			in.setJmsBinding(jw);
			ret.add(in);
			
			Boolean isLocal = DomainManager.isLocal(sc.getDomainId());
			ret.add(isLocal);
			
			//ret.add(e)
			res.setCode("0");
			res.setResponse(ret);
		
		} catch (BifangException e) {
			throw e;	
		}catch (Exception e) {
			throw new BifangException(BifangErrorCode.UNKNOWN_ERROR,e.getMessage(),e);		
		}
		return res;
		
	}

}
